package com.yuedou.jylt.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import com.squareup.picasso.Picasso;
import com.yuedou.jylt.R;
import com.yuedou.jylt.constant.MApplication;
import com.yuedou.jylt.dto.ResponseObj;
import com.yuedou.jylt.dto.param.InitDeviceParam;
import com.yuedou.jylt.http.RequestServices;
import com.yuedou.jylt.http.RequestUtils;
import com.yuedou.jylt.sharedpref.SharedPrefConstant;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SettingActivity extends AppCompatActivity {

    @BindView(R.id.cpu_serial_num)
    AppCompatEditText cpu_serial_num;
    @BindView(R.id.btn_init)
    Button btn_init;
    @BindView(R.id.btn_network)
    Button btn_network;
    @BindView(R.id.img_qrcode)
    ImageView img_qrcode;
    @BindView(R.id.btn_download)
    Button btnDownload;

    private Context context;
    private Unbinder unbinder;

    private String cpuInfo = "", serialNumber = "";
    Handler h = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        unbinder = ButterKnife.bind(this);

        initConstants();
        initView();
    }

    private void initConstants() {
        context = SettingActivity.this;
    }


    /**
     *  界面初始化
     */
    private void initView(){
        //读取cpu 序列号
//        DeviceUtil deviceUtil = new DeviceUtil();
//        this.deviceSn = deviceUtil.getCpuInfo();
        this.serialNumber = MApplication.mSharedPref.getSharePrefString(SharedPrefConstant.DEVICE_SERIAL_NUM);
        System.out.println("serialNumber: " + this.serialNumber);
        cpu_serial_num.setText(serialNumber);
    }


    @OnClick({R.id.btn_init, R.id.btn_network, R.id.btn_download})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_init:
                // 系统通知
                onHttpInitClick();
                break;
            case R.id.btn_network:
                // 系统通知
                Intent i = new Intent();
                if(android.os.Build.VERSION.SDK_INT >= 11){
                    //Honeycomb
                    i.setClassName("com.android.settings", "com.android.settings.Settings$WifiSettingsActivity");
                }else{
                    //other versions
                    i.setClassName("com.android.settings"
                            , "com.android.settings.wifi.WifiSettings");
                }
                startActivity(i);
                break;
            case R.id.btn_download:// 下载素材

                break;
        }
    }


    /**
     *  初始化按钮网络请求
     */
    private void onHttpInitClick(){

        //步骤4:创建Retrofit对象
//        var mSerialPortManager:SerialPortManager?=null
        RequestServices requestServices = RequestUtils.getRetrofit().create(RequestServices.class);
        //对 发送请求 进行封装
        //
        InitDeviceParam deviceParam = new InitDeviceParam();
        deviceParam.setCpu_id(this.serialNumber);
        Call<ResponseObj> call = requestServices.deviceInit(deviceParam);
        //步骤6:发送网络请求(异步)
        call.enqueue(new Callback<ResponseObj>() {
            @Override
            public void onResponse(Call<ResponseObj> call, @NonNull Response<ResponseObj> response) {
                Log.i("初始化设备 返回数据",response.toString());
                ResponseObj responseObj = response.body();
                System.out.println("data: " + responseObj.getData().toString());
                System.out.println();
                if (responseObj != null) {
                    if (responseObj.getCode() == 0) {
                        // 成功
                        String qrCode = responseObj.getData().get("qrCode").toString();
                        String deviceSn = responseObj.getData().get("deviceSn").toString();
                        MApplication.mSharedPref.putSharePrefString(SharedPrefConstant.DEVICE_QRCODE, qrCode);
                        MApplication.mSharedPref.putSharePrefString(SharedPrefConstant.DEVICE_SN, deviceSn);
                        Picasso.with(context).load(qrCode).into(img_qrcode);
                    }else if(responseObj.getCode() == 2){//已存在了
                        String qrCode = responseObj.getData().get("qrCode").toString();
                        String deviceSn = responseObj.getData().get("deviceSn").toString();
                        MApplication.mSharedPref.putSharePrefString(SharedPrefConstant.DEVICE_QRCODE, qrCode);
                        MApplication.mSharedPref.putSharePrefString(SharedPrefConstant.DEVICE_SN, deviceSn);
                        Picasso.with(context).load(qrCode).into(img_qrcode);
                    }
                    Toast.makeText(context, responseObj.getMsg(),Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<ResponseObj> call, Throwable t) {
                t.printStackTrace();
                Log.d("设备初始化 失败", t.getMessage());
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }
}